package bb.lanxing.lib.devices.bryton.ncs;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.legacy.content.WakefulBroadcastReceiver;
import java.util.Calendar;

public class PeriodicExtensionRefreshReceiver extends WakefulBroadcastReceiver {
    private static final String ACTION_PERIODIC_ALARM = "com.brytonsport.ncs.action.PERIODIC_ALARM";
    public static final int MINUTES_MILLIS = 60000;
    public static final int SECONDS_MILLIS = 1000;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || !ACTION_PERIODIC_ALARM.equals(intent.getAction())) {
            return;
        }
        startWakefulService(context, getUpdateAllExtensionsIntent(context, 2));
    }

    private static Intent getUpdateAllExtensionsIntent(Context context, int i) {
        return new Intent(context, NotificationCenterService.class).setAction(NotificationCenterService.ACTION_UPDATE_EXTENSIONS).putExtra(NotificationCenterService.EXTRA_UPDATE_REASON, i);
    }

    public static void updateExtensionsAndEnsurePeriodicRefresh(Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(NotificationCompat.CATEGORY_ALARM);
        context.startService(getUpdateAllExtensionsIntent(context, 6));
        PendingIntent broadcast = PendingIntent.getBroadcast(context, 0, new Intent(context, PeriodicExtensionRefreshReceiver.class).setAction(ACTION_PERIODIC_ALARM), Build.VERSION.SDK_INT >= 23 ? 201326592 : 134217728);
        alarmManager.cancel(broadcast);
        alarmManager.setInexactRepeating(0, Calendar.getInstance().getTimeInMillis() + 900000, 1800000L, broadcast);
    }
}
